package com.sznaner.tool.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;


//需要添加已下权限
// <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public class NetworkUtil {

    public static int TYPE_WIFI = 1;
    public static int TYPE_ETHERNET = 9;


    public static boolean isConnected() {
        boolean state = false;
        ConnectivityManager cm =
                (ConnectivityManager) NRTool.context().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) {
            if(activeNetwork.getType() ==  ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_ETHERNET){

                LogUtil.debugLog("网络类型:"+activeNetwork.getType()+",状态:"+activeNetwork.isConnected());
                state = activeNetwork.isConnected();
            }
        }
        return state;
    }


    public static int connecteType(){
        int type = TYPE_WIFI;
        ConnectivityManager cm =
                (ConnectivityManager) NRTool.context().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) {
            type = activeNetwork.getType();
        }
        return type;
    }

}
